package com.stark.wechat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import com.stark.wechat.form.Result;
import com.stark.wechat.service.impl.MenuServiceImpl;
import com.stark.wechat.util.SysTools;
import com.stark.wechat.util.WechatUtil;

/**
 * 自定义菜单控制器。
 * @author Ben
 * @since 1.0.0
 * @version 1.0.0, 2017/8/5
 */
@Controller
public class MenuController {
	@Autowired
	private MenuServiceImpl menuService;
	
	/**
	 * 更新自定义菜单。
	 * @return 视图。
	 * @throws Exception 请求数据时抛出异常。
	 */
	@GetMapping("/menu/update")
	public ModelAndView update() throws Exception {
		String menuJsonStr = this.menuService.generateMenuJsonStr();
		Result result = WechatUtil.updateCustomMenu(SysTools.getAccessToken(), menuJsonStr);
		
		if (result.isSuccess()) {
			ModelAndView mav = new ModelAndView("info/success");
			mav.addObject("msg", "更新自定义菜单成功");
			return mav;
		}
		
		ModelAndView mav = new ModelAndView("info/error");
		mav.addObject("msg", "更新自定义菜单失败！错误代码：" + result.getErrcode() + "，错误信息：" + result.getErrmsg());
		return mav;
	}
}
